<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>

    <style>
        body {
            margin: 0;
            padding: 30px;
        }
    </style>

    <!-- 导入axios包 -->
    <script src="./node_modules/axios/dist/axios.min.js"></script>

</head>

<body>

    <h1>编辑用户</h1>
    <label for="username">编辑用户：</label>
    <input type="text" id="username">
    <br>

    <label for="password">密码：</label>
    <input type="text" id="password">
    <br>

    <button>提交</button>

    <script>
        //加载页面的时候会将url中的详细信息保存在location.search中，调用URLSearchParams对象使用来获取到具体id
        console.log(location.search)
        const params = new URLSearchParams(location.search)
        const id = params.get('id')


        axios({
            url: `http://localhost:3000/users/${id}`,
            method: 'get'
        }).then(res => {
            console.log(res)
            const data = res.data
            // 继续回显操作
            const username = document.querySelector('#username')
            const password = document.querySelector('#password')
            username.value = data.username
            password.value = data.password
        })


        const button = document.querySelector('button')
        button.addEventListener('click', function () {

            const username = document.querySelector('#username').value
            const password = document.querySelector('#password').value



            axios({
                url: `http://localhost:3000/users/${id}`,
                method: 'put',
                data: { username: username, password: password }

            }).then(location.href = './index.html')



        })









    </script>








</body>

</html>